#include "jconcurrent.h"
#include <JXQCore/JMutex>
#include <JXQCore/JDebug>

namespace JXQ
{

class JConcurrentPrivate
{
};

JThreadPool *gThreadPool = &JThreadPool::globalInstance();
std::mutex gMutex;

/*-----------------------------------------------------------------------------*/

JConcurrent::JConcurrent():
	d_ptr(new JConcurrentPrivate())
{

}

JConcurrent &JConcurrent::globalInstance()
{
	static JConcurrent jc;
	return jc;
}

void JConcurrent::setThreadPool(JThreadPool *pool)
{
	if( pool == nullptr )
	{
		jError() << "warning: JSqlEngine::setThreadPool: the JThreadPool is NULL";
		return ;
	}
	J_MUTEX_LOCKER(gMutex);
	if( gThreadPool != &JThreadPool::globalInstance() and gThreadPool != nullptr )
		delete gThreadPool;
	gThreadPool = pool;
}

JThreadPool &JConcurrent::threadPool()
{
	J_MUTEX_LOCKER(gMutex);
	return *gThreadPool;
}

} //namespace JXQ
